package com.woniu40.petai_manager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Role;
import com.woniu40.petai_manager.mapper.PermissionMapper;
import com.woniu40.petai_manager.mapper.RoleMapper;
import com.woniu40.petai_manager.vo.PermissionVo;
import com.woniu40.petai_manager.vo.RoleVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleService {
    @Autowired
    RoleMapper roleMapper;
    public List<Role> findRoleAll(){
        return roleMapper.selectAll();
    }
    public List<Role> findRoleByManager(Integer ManagerId){
        return roleMapper.selectAllByManager(ManagerId);
    }



    @Autowired
    PermissionMapper permissionMapper;

    public RoleVo findRole(Integer roleId){
        RoleVo roleVo = roleMapper.selectRoleVo(roleId);
        return roleVo;
    }

    public PageInfo<Role> findAll(String roleName, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roles = roleMapper.selectAll1(roleName);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return pageInfo;
    }

    @Transactional
    public int addRole(RoleVo roleVo){
        int insert = roleMapper.insert(roleVo);
        Integer roleId = roleVo.getRoleId();
        if( roleVo.getPerList().size() != 0){
            int i = roleMapper.insertRolePermission(roleId, roleVo.getPerList(), roleVo.getInsertTime());
        }
        return insert;
    }

    public RoleVo findRoleVo(Integer roleId){
        RoleVo roleVo = roleMapper.selectRoleVo(roleId);
        return roleVo;
    }

    @Transactional
    public int removeRoleAndPermission(Integer roleId){
        int delete = roleMapper.deleteByPrimaryKey(roleId);
        int i = roleMapper.deleteRolePermission(roleId);
        return delete ;
    }

    @Transactional
    public int modifyRoveAndPermission(RoleVo roleVo){
        int i = roleMapper.updateRole(roleVo);
        roleMapper.deleteRolePermission(roleVo.getRoleId());
        if(roleVo.getPerList().size() != 0){
            roleMapper.insertRolePermission(roleVo.getRoleId(),roleVo.getPerList(),roleVo.getUpdateTime());
        }
        return i;
    }



}
